//
//  NWToast.m
//  mesu
//
//  Created by dl on 2018/7/15.
//  Copyright © 2018年 gdmesu. All rights reserved.
//

#import "NWToast.h"
#import <MBProgressHUD/MBProgressHUD.h>
#import <objc/runtime.h>
#import "AppDelegate.h"
#import "UIColor+NWHex.h"

static char ProgressHUDKey;

@implementation NWToast

+ (void)showToast:(NSString *)toast
{
    [self showToast:toast afterDelay:1.3];
}

+ (void)showLoadingToast:(NSString *)toast
{
    [self dismissLoading];
    UIViewController *visibleVC = self.currentTopVisibleViewController;
    UIActivityIndicatorView *loadingView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    loadingView.frame = CGRectMake(0.f, 0.f, 40.f, 40.f);
    [loadingView startAnimating];
    
    MBProgressHUD *hud  = [MBProgressHUD showHUDAddedTo:visibleVC.view animated:YES];
    hud.labelText = toast;
    hud.cornerRadius = 8;
    hud.labelFont = [UIFont systemFontOfSize:11.0];
    hud.labelColor = [UIColor nw_colorWithHex:0x8f8f8f];
    hud.customView = loadingView;
    hud.mode = MBProgressHUDModeCustomView;
    hud.margin = 12.f;
    hud.removeFromSuperViewOnHide = YES;
    hud.yOffset = 32;
    [self setVCProgressHUD:visibleVC hud:hud];
}

+ (void)dismissLoading
{
    MBProgressHUD *progressHUD = [self progressHUDOfVC:self.currentTopVisibleViewController];
    if (progressHUD) {
        [progressHUD hide:YES];
    }
}

+ (void)showToast:(NSString *)toast afterDelay:(NSTimeInterval)delay
{
    UIViewController *visibleVC = self.currentTopVisibleViewController;
    [self showToastAddToView:visibleVC.view toast:toast delay:delay];
}

+ (void)showWindowToast:(NSString *)toast afterDelay:(NSTimeInterval)delay {
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [self showToastAddToView:appDelegate.window toast:toast delay:delay];
}

+ (void)showToastAddToView:(UIView *)view toast:(NSString *)toast delay:(NSTimeInterval)delay
{
    
    if ( ![toast isKindOfClass:[NSString class]] ) {
        return;
    }
    if ( !toast.length ) {
        return;
    }
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    // 采用自定义的方式，支持多行文本
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
    label.adjustsFontSizeToFitWidth = NO;
    label.textAlignment = NSTextAlignmentCenter;
    label.opaque = NO;
    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont boldSystemFontOfSize:16.0];
    label.text = toast;
    label.numberOfLines = 2;
    CGSize size = [toast boundingRectWithSize:CGSizeMake(300, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:16.0]} context:nil].size;
    label.frame = CGRectMake(0, 0, size.width + 4, size.height + 4);
    hud.customView = label;
    hud.mode = MBProgressHUDModeCustomView;
    hud.margin = 10.f;
    hud.yOffset = -100.f;
    hud.removeFromSuperViewOnHide = YES;
    
    [hud hide:YES afterDelay:delay];
}

+ (MBProgressHUD*)progressHUDOfVC:(UIViewController*)vc{
    return objc_getAssociatedObject(vc, &ProgressHUDKey);
}

+ (void)setVCProgressHUD:(UIViewController*)vc hud:(MBProgressHUD*)hud{
    objc_setAssociatedObject(vc, &ProgressHUDKey, hud, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

+(UIViewController*)currentTopVisibleViewController{
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    UINavigationController *rootViewController = (UINavigationController *)appDelegate.window.rootViewController;
    UIViewController *topViewController = rootViewController.topViewController;
    if ([rootViewController.topViewController isKindOfClass:[UITabBarController class]]) {
        UINavigationController *navVC = (UINavigationController *)[(UITabBarController *) rootViewController.topViewController selectedViewController];
        topViewController = navVC.topViewController;
    }
    while (topViewController.presentedViewController
           && ![topViewController.presentedViewController isKindOfClass:[UINavigationController class]]) {
        topViewController = topViewController.presentedViewController;
    }
    
    while (topViewController.presentedViewController
           && [topViewController.presentedViewController isKindOfClass:[UINavigationController class]]) {
        topViewController = [(UINavigationController *)topViewController.presentedViewController topViewController];
    }
    return topViewController;
}

@end
